/*
 * @Author: MiNine
 * @Date: 2020-09-18 17:52:44
 * @LastEditors: MiNine
 * @LastEditTime: 2020-09-27 10:31:48
 * @Description: 
 */
import Vue from "vue";
import VueRouter from "vue-router";
// import Home from '../views/Home.vue'

Vue.use(VueRouter);

const routes = [{
        path: "/",
        redirect: "/Entry", //目前仅供测试
    },
    {
        path: "/Entry",
        name: "Entry",
        component: () => import("../views/Entry/Entry.vue"),
        redirect: "/Entry/login",
        children: [{
            path: "login",
            name: "login",
            component: () => import("../components/Entry.components/login.vue"),
        }, ],
    },
    {
        path: "/system",
        name: "System",
        component: () => import("../views/System/System.vue"),
        redirect: "system/insureOrder",
        children: [{
                path: "insureOrder",
                name: "InsureOrder",
                component: () => import("../views/System/InsureOrder/InsureOrder.vue"),
                children:[{
                    path: "manage",
                    name: "manage",
                    component: () => import("../views/System/InsureOrder/InsureOrder.vue"),
                }]
            },
            {
                path: "clients",
                name: "Clients",
                component: () => import("../views/client/Clients"),
                // children: [{
                //     path: "add",
                //     name: "Add",
                //     component: () => import("../components/InsureOrder.components/Add.vue"),
                // }],
            },
            {
                path: "admin",
                name: "admin",
                component: () => import("../views/System/Admin/Admin.vue"),
                redirect: "admin/userslist",
                children: [{
                        path: "modOrderStatus",
                        name: "modOrderStatus",
                        component: () =>
                            import("../components/Admin.components/ModOrderStatus.vue"),
                    },
                    {
                        path: "usersList",
                        name: "UsersList",
                        component: () => import("../components/Admin.components/UsersList.vue"),
                    },
                    {
                        path: "adduser",
                        name: "AddUser",
                        component: () => import("../components/Admin.components/AddUser.vue"),
                    },
                ],
            },
            {
                path: "insureProduct",
                name: "insureProduct",
                redirect: "/system/insureProduct/getItems",
                component: () => import("../views/System/InsureProduct/InsureProduct.vue"),
                children: [{
                        path: "getItems",
                        name: "getItem",
                        component: () =>
                            import("../components/insureProduct.components/GetItems.vue"),
                    },
                    {
                        path: "addItem",
                        name: "addItem",
                        component: () =>
                            import("../components/insureProduct.components/AddItem.vue"),
                    },
                    {
                        path: "modItem/:id",
                        name: "modItem",
                        props: true,
                        component: () =>
                            import("../components/insureProduct.components/ModItem.vue"),
                    },
                ],
            },
            {
                path: "claimOrder",
                name: "ClaimOrder",
                component: () => import("../views/System/ClaimOrder/ClaimOrder.vue"),
                children: [{
                        path: "getClaims",
                        name: "GetClaims",
                        component: () =>
                            import("../components/claimOrder.components/GetClaims.vue"),
                    },
                    {
                        path: "modClaim/:id",
                        name: "modClaim",
                        props: true,
                        component: () => import("../components/claimOrder.components/ModClaim.vue"),
                    },
                    {
                        path: "addCompensation",
                        name: "AddCompensation",
                        props: true,
                        component: () =>
                            import("../components/claimOrder.components/AddCompensation.vue"),
                    },
                    {
                        path: "getCompensations/:_id",
                        name: "GetCompensations",
                        component: () =>
                            import("../components/claimOrder.components/GetCompensations.vue"),
                    },
                    {
                        path: "modCompensation",
                        name: "ModCompensation",
                        component: () =>
                            import("../components/claimOrder.components/ModCompensation.vue"),
                    },
                ],
            },
            {
                path: "insureSort",
                name: "InsureSort",
                component: () => import("../views/System/InsureSort/InsureSort.vue"),
                children:[{
                    path: "manage",
                    name: "manage",
                    component: () => import("../views/System/InsureSort/InsureSort.vue"),
                }]
            },
            {
                path: "userCenter",
                name: "UserCenter",
                component: () => import("../views/System/userCenter/userCenter.vue"),
                redirect: "/system/UserCenter/modData",
                children: [{
                    path: "modData",
                    name: "modData",
                    component: () =>
                        import("../components/UserCenter.components/modData.vue"),
                }]
            },
        ],

    },
];

//解决路由重复问题
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err);
};

const router = new VueRouter({
    routes,
});

//全局守卫
router.beforeEach((to, from, next) => {
    if (to.path.includes('system')) {
        const token = localStorage.token;
        console.log(Vue.prototype)
        if (token) {
            next();
        } else {
            alert('登录失效,请先登陆');
            next('/Entry/login');
        }
    } else {
        next();
    }
})

export default router;